package healthyPointsManager.component
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.containers.Panel;
	import mx.controls.Button;
	import mx.controls.Label;
	
	[Event(name="buttonClick", type="flash.events.Event")]//adds a new Event that can be set or listened to when making a new buttonPanel

	public class ButtonPanel extends Panel
	{
		[Bindable]
		public var buttonLabel:String;//adds two new properties to codeCompletion that can be set at the time of making a new ButtonPanel
		
		[Bindable]
		public var buttonPaddingHeight:Number = 10;//sets a default
		[Bindable]
		public var buttonPaddingWidth:Number = 10;//sets a default
		[Bindable]
		public var buttonHeight:Number;
		[Bindable]
		public var buttonWidth:Number;		
		private var _localButton:Button;
		private var _localVersion:Label;
		[Bindable]
		private var _labelVersion:String;
		
		public function ButtonPanel()
		{
			super();
		}
		
		public function get localButton():Button{
			return _localButton;
		}
		
		public function set localButton(localButton:Button):void{
			this._localButton = localButton;
		}
		
		public function get localVersion():Label{
			return _localVersion;
		}
		
		public function set localVersion(localVersion:Label):void{
			this._localVersion = localVersion;
		}
		
		public function get labelVersion():String{
			return _labelVersion;
		}
		
		public function set labelVersion(labelVersion:String):void{
			var update:Boolean = _labelVersion == null;
			this._labelVersion = labelVersion;
			if(update){
				createSubChild();
			}
		}
		
		//modify the function that adds childern objects to a normal panel by including the creation of a button
		protected override function createChildren():void{
			super.createChildren();
			
			if(buttonLabel){
				_localButton = new Button();
				_localButton.label = buttonLabel;
				_localButton.visible = true;
				_localButton.includeInLayout = true;
				_localButton.addEventListener(MouseEvent.CLICK, buttonClickHandler);
				_localButton.tabEnabled = false;//prevent user from tabbing to the visual aspect of the button
				_localButton.enabled = false;//this starts out disabled because upon loading no new data should need to be saved
				rawChildren.addChild(_localButton);
			}
			if(labelVersion){
				createSubChild();
			}
								
		}
		
		private function createSubChild():void{						
				_localVersion = new Label();
				_localVersion.text = labelVersion;
				_localVersion.visible = true;
				_localVersion.includeInLayout = true;
				_localVersion.tabEnabled = false;
				rawChildren.addChild(_localVersion);
				
		}
		
		/*This code is needed inorder to have the button actually show up.*/
		protected override function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
			super.updateDisplayList(unscaledWidth, unscaledHeight);			
			var x:int = width - (140 + buttonPaddingWidth);
			if(_localButton != null){
				x = width - (_localButton.width + buttonPaddingWidth);//get the width excluding the width of button and its padding
				
				if(isNaN(buttonWidth)){
					_localButton.width = _localButton.measuredWidth;	
				}else{
					_localButton.width = buttonWidth;
				}
				if(isNaN(buttonHeight)){
					_localButton.height = _localButton.measuredHeight;	
				}else{
					_localButton.height = buttonHeight;
				}
				setStyle('headerHeight', _localButton.height + buttonPaddingHeight);
				
				_localButton.move(x, 5);
			}
			if(_localVersion != null){
				
				var y:int = height - (_localVersion.height + buttonPaddingHeight);
				_localVersion.width = _localVersion.measuredWidth;
				_localVersion.height = _localVersion.measuredHeight;
				setStyle('footerHeight', _localVersion.height + buttonPaddingHeight);			
				_localVersion.move(x, y);
			}
		}
		
		private function buttonClickHandler(event:MouseEvent):void{
			this.dispatchEvent(new Event('buttonClick', false));
		}
		
	}
}